document.addEventListener('DOMContentLoaded', () => {
    const menuItems = document.querySelectorAll('.sidebar-menu li');

    menuItems.forEach(item => {
        item.addEventListener('click', (event) => {
            event.stopPropagation();
            if (item.classList.contains('has-submenu')) {
                const submenu = item.querySelector('.submenu');
                const isOpen = item.classList.contains('active');
                closeAllSubmenus();
                if (!isOpen) {
                    item.classList.add('active');
                    submenu.style.maxHeight = submenu.scrollHeight + 'px';
                    submenu.style.opacity = '1';
                }
            } else {
                closeAllSubmenus();
                item.classList.add('active');
            }
        });
    });

    function closeAllSubmenus() {
        menuItems.forEach(item => {
            if (item.classList.contains('has-submenu')) {
                const submenu = item.querySelector('.submenu');
                item.classList.remove('active');
                submenu.style.maxHeight = '0';
                submenu.style.opacity = '0';
            } else {
                item.classList.remove('active');
            }
        });
    }
});
